package com.liuceng.zen.web.auth.utils;

import org.apache.commons.lang3.SerializationUtils;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.session.Session;

import java.io.Serializable;

public class SerializeUtils extends SerializationUtils {
    /**
     * 反序列化
     * 
     * @param obj
     * @return
     */
    public static String serializeToString(Serializable obj) {
        try {
            byte[] value = serialize(obj);
            return Base64.encodeToString(value);
        } catch (Exception e) {
            throw new RuntimeException("serialize session error", e);
        }
    }

    /**
     * 序列化
     * 
     * @param base64
     * @return
     */
    public static Session deserializeFromString(String base64) {
        try {
            byte[] objectData = Base64.decode(base64);
            return deserialize(objectData);
        } catch (Exception e) {
            throw new RuntimeException("deserialize session error", e);
        }
    }

    /*
     * public static <T> Collection<T>
     * deserializeFromStringController(Collection<String> base64s) { try {
     * List<T> list = Lists.newLinkedList(); for (String base64 : base64s) {
     * byte[] objectData = Base64.decode(base64); T t = deserialize(objectData);
     * list.add(t); } return list; } catch (Exception e) { throw new
     * RuntimeException("deserialize session error", e); }
     */
    // }
    /**
     * 序列化
     * 
     * @param object
     * @return
     */
    /*
     * public static byte[] serialize(Object object) { ObjectOutputStream oos =
     * null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new
     * ByteArrayOutputStream(); oos = new ObjectOutputStream(baos);
     * oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes;
     * } catch (Exception e) { e.printStackTrace(); } return null; }
     */

    /**
     * 反序列化
     * 
     * @param bytes
     * @return
     */
    /*
     * public static Object deserialize(byte[] bytes) { ByteArrayInputStream
     * bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes);
     * ObjectInputStream ois = new ObjectInputStream(bais); return
     * ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return
     * null; }
     */
}
